import { Component, OnInit, Input, Injector } from '@angular/core';
import { ModalComponentBase } from '@shared/component-base/modal-component-base';
import { CreateUserDto, UserServiceProxy, UserDto, ListResultDtoOfRoleDto } from '@shared/service-proxies/service-proxies';
import { TokenService } from '@delon/auth';
import { AppSessionService } from '@shared/session/app-session.service';
import { AppConsts } from '@shared/AppConsts';
import { SFSchema, SFSchemaEnumType } from '@delon/form';
import { mergeMap as _observableMergeMap, catchError as _observableCatch } from 'rxjs/operators';
import { of, Observable } from 'rxjs';

@Component({
  selector: 'app-create-or-update-user',
  templateUrl: './create-or-update-user.component.html',
  styles: []
})
export class CreateOrUpdateUserComponent extends ModalComponentBase implements OnInit {

  i: CreateUserDto;
  @Input() id: number;
  loading: boolean = false;
  constructor(
    public userServiceProxy: UserServiceProxy,
    // public tokenService: TokenService,
    public appSessionService: AppSessionService,
    injector: Injector) {
    super(injector);
  }

  ngOnInit() {
    this.i = new CreateUserDto();
    this.i.isActive = true;
    if (this.id) {
      //修改操作
      this.loading = true;
      this.userServiceProxy.get(this.id).finally(() => {
        this.loading = false;
      }).subscribe(x => {
        this.i = CreateUserDto.fromJS(x);

        // if (x.photoUrl) {
        //   this.imageSource.push({
        //     uid: -1,
        //     name: x.photoUrl,
        //     status: 'done',
        //     url: x.photoUrl,
        //   });
        // }

        // //delete (this.schema.properties["lineIds"]);
        // delete (this.schema.properties["password"]);
        //console.log(;

      })
    } else { }
  }

  save(v: CreateUserDto) {

    this.loading = true;
    if (this.id) {
      var model = UserDto.fromJS(v);
      model.id = this.id;

      this.userServiceProxy.update(model)
        .finally(() => {
          this.loading = false;
        }).subscribe((x) => {
          abp.notify.success('修改成功');
          this.success(1);
        });
    } else {
      this.userServiceProxy.create(v)
        .finally(() => {
          this.loading = false;
        }).subscribe((x) => {
          abp.notify.success('添加成功');
          this.success(1);
        });
    }


  }

  imageSource: SFSchemaEnumType[] = [];
  lineIds: SFSchemaEnumType[] = [];
  schema: SFSchema = {
    type: "object",
    ui: { grid: { xs: 24, md: 12 } },
    properties: {
      // photo: {
      //   type: 'string',
      //   title: '头像',
      //   enum: this.imageSource,
      //   ui: {
      //     // type: 'drag',
      //     widget: 'upload',
      //     //listType: 'picture',
      //     resReName: 'result.fileName',
      //     action: AppConsts.remoteServiceBaseUrl + '/file/UploadProfilePicture',
      //     fileType: 'image/png,image/jpeg,image/gif,image/bmp'
      //   }
      // },
      userName: {
        type: "string",
        title: '工号',
        maxLength: 32
      },
      emailAddress: {
        type: "string",
        title: '邮箱',
        format: 'email',
        maxLength: 32
      },
      name: {
        type: 'string',
        title: '姓名',
        maxLength: 32
      },
      password: {
        type: 'string',
        maxLength: 32,
        title: '密码',
        ui: { type: 'password' }
      },
      isActive: {
        title: '启用',
        type: 'boolean'
      },
      // lineIds: {
      //   type: 'string',
      //   title: '所属线路',
      //   enum: this.lineIds,
      //   ui: {
      //     placeholder: '选择一条或多条线路',
      //     widget: 'select',
      //     mode: 'tags'
      //   },
      //   default: null
      // },
      roleNames: {
        title: '角色',
        type: "string",
        ui: {
          placeholder: '选择一个或多个角色',
          widget: 'select', mode: 'tags',
          asyncData: () => {
            return this.userServiceProxy.getRoles().pipe(_observableMergeMap((response: ListResultDtoOfRoleDto) => {
              let t: SFSchemaEnumType[] = []
              response.items.map(x => {
                t.push({
                  label: x.displayName,
                  value: x.name
                })
              });

              return of(t);
            }))
          },
          // grid: { xs: 24, md: 24, spanLabel: 2 }
        }
      }
    },
    required: ['userName', 'emailAddress', 'name']
  };

}
